﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SF.Code.Extension.WinAPI;
using SG.Ling.AutoTranslator.Common;
using SG.Ling.AutoTranslator.Translation;

namespace SG.Ling.AutoTranslator.App
{
    public partial class Program
    {
        private const string NoShowFlag = "noshow";
        private const string NoStartFlag = "nostart";

        private static Program RunningProgram { get; set; }

        private Program(ICollection<string> args)
        {
            Context = new ArgumentParser(args);
        }

        private ArgumentParser Context { get; set; }

        private UserForm TranslatorForm { get; set; }

        private BatchTranslator SystemBatchTranslator { get { return TranslatorForm != null ? TranslatorForm.SystemTranslator : null; } }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            RunningProgram = new Program(args);
            RunningProgram.Start();
        }

        private void Start()
        {
            var alreadyRunningWindow = CommandGroupEx.FindApplicationWindow();
            if (alreadyRunningWindow == IntPtr.Zero) // just me, so run!
            {
                if (Context.HasFlag(NoStartFlag))
                    return;

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                var main = Infrastructure.MainPumpForm = new InvisibleForm{ Text = Infrastructure.MainWindowName };
                TranslatorForm = new UserForm();
                StartHttpServer();
                main.OnWinProcMessage += OnWindowMessage;
                main.OnWinProcMessage += OnDebugMessage;
                main.OnWinProcMessage += OnLogoffMessage;
                main.Load += MainLoad;
                main.Closing += MainClosing;
                Application.Run(main);
            }
            else // switch to the first instance and exit
            {
                var group = CommandGroup.Application;
                var command = Command.Show;

                var commands = Context.Commands().ToArray();

                if (commands.Length >= 2)
                {
                    Enum.TryParse(commands[0].Text, true, out group);
                    Enum.TryParse(commands[1].Text, true, out command);
                }
                group.SendCommand(command, alreadyRunningWindow);
            }
        }

        void MainClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            CloseSubForms();
        }

        void MainLoad(object sender, EventArgs e)
        {
            if (Context.HasFlag(NoShowFlag))
                return;

            CommandGroup.Application.SendCommand(Command.Show, Infrastructure.MainPumpForm.Handle);
        }

        private void Close()
        {
            Infrastructure.NotifyBeforeClose();

            CloseSubForms();

            Infrastructure.MainPumpForm.Close();
        }

        private void CloseSubForms()
        {
            if (TranslatorForm == null) return;

            TranslatorForm.CloseFromMenu = true;
            TranslatorForm.Close();
            TranslatorForm = null;
        }
    }
}
